package com.JobFind.logic.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import com.JobFind.DAO.ICompanyDAO;
import com.JobFind.DTO.CompanyDTO;
import com.JobFind.DTO.CompanyShowDTO;
import com.JobFind.DTO.CompanySimplyDTO;
import com.JobFind.DTO.LogDTO;
import com.JobFind.logic.ICompanyLogic;
import com.JobFind.logic.ILogLogic;
import com.JobFind.model.TbCompany;
import com.JobFind.model.TbLog;
import com.JobFind.model.TbUser;

@Component
public class CompanyLogic implements ICompanyLogic {

	private ICompanyDAO companyDAO;
    private ICompanyLogic companyLogic;
    @Resource
    private ILogLogic logLogic;
	public ICompanyDAO getCompanyrDAO() {
		return companyDAO;
	}

	public ICompanyDAO getCompanyDAO() {
		return companyDAO;
	}

	@Resource
	public void setCompanyDAO(ICompanyDAO companyDAO) {
		this.companyDAO = companyDAO;
	}

	public ICompanyLogic getCompanyLogic() {
		return companyLogic;
	}

	@Resource
	public void setCompanyLogic(ICompanyLogic companyLogic) {
		this.companyLogic = companyLogic;
	}




	@Override
	@Transactional
	public Integer loginCheck(String companyLoginName, String companyPwd) throws Exception {
		Integer result = 0;
		try {
			List<TbCompany> companylist = companyDAO.loginCheck(companyLoginName);
			if (companylist == null) {
				result = 0;
			} else if (companylist.size() == 0) {
				result = 0;
			} else {
				TbCompany companybean = (TbCompany) companylist.get(0);
				List<CompanyDTO> companyDTOList = new ArrayList<CompanyDTO>();
				for(TbCompany company:companylist){
					companyDTOList.add(company.toDTO());
				}
				String companyString = JSONArray.fromObject(companyDTOList).toString();
				if (!companyPwd.equals(companybean.getCompanyPwd())) {
					result = 0;
				} else {
					HttpServletRequest request = ServletActionContext.getRequest();
					HttpSession loginuserSession = request.getSession();
					loginuserSession.setAttribute("loginusersession", companyString);
					result = companybean.getCompanyId();
					LogDTO logDTO = new LogDTO();
					logDTO.setLogIp(request.getRemoteAddr());
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					logDTO.setLogTime(df.format(new Date()).toString());
					logDTO.setUserId(companylist.get(0).getCompanyId());
					logDTO.setUserType(2);
					logLogic.addLog(logDTO.toDAO());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = -2;
		}
		return result;
	}

	@Override
	@Transactional
	public String getCompanyInfoById(Integer companyId,Integer set) throws Exception {
		String result="";
		TbCompany companyBean=null;
		CompanySimplyDTO companySimplyDTO = new CompanySimplyDTO();
		CompanyDTO companyDTO = new CompanyDTO();
		try {
			companyBean=this.companyDAO.getCompanyInfoById(companyId);
			if(set == 1){
				companySimplyDTO=companyBean.toSimplyDTO();
				result = JSONArray.fromObject(companySimplyDTO).toString();
			}else{
				companyDTO = companyBean.toDTO();
				result = JSONArray.fromObject(companyDTO).toString();
			}
		} catch (Exception e) {
		  e.printStackTrace();
		  result = "";
		}
		return result;
	}

	@Override
	@Transactional
	public Integer updateCompanyInfo(CompanyDTO companyDTO) throws Exception {
		Integer result = 0;
		try {
			result = this.companyDAO.updateCompanyInfo(companyDTO.toDAO());
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer logoutCheck() throws Exception {
		Integer result = 0;
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.getSession().removeAttribute("loginusersession");
			request.getSession().invalidate();
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer modifyCompanyPwd(Integer companyId, String companyPwd)
			throws Exception {
		Integer result = 0;
		try {
			result = this.companyDAO.modifyCompanyPwd(companyId, companyPwd);
		} catch (Exception e) {
		  e.printStackTrace();
		  result = 0;
		}
		return result;
	}

	@Override
	public String getCompanySession() throws Exception {
		String result = "";
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpSession loginuserSession = request.getSession();
			result = loginuserSession.getAttribute("loginusersession").toString();
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}

	@Override
	@Transactional
	public Integer updateCompanyLogo(String companyLogo,Integer companyId) throws Exception {
		Integer result = 0;
		try {
			result = this.companyDAO.updateCompanyLogo(companyLogo,companyId);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public Integer addCompany(CompanyDTO companyDTO) throws Exception {
		Integer result = 0;
		try {
			List<TbCompany> companylist1 = companyDAO.loginCheck(companyDTO.getCompanyLoginName());
			List<TbCompany> companylist2 = companyDAO.getComInfoByName(companyDTO.getCompanyName());
			if(companylist1.size()!=0){
				result = -1;//登录名已存在;
			}else if(companylist2.size()!=0){
				result = -2;//公司名称已存在;
			}
			else{
				result = this.companyDAO.addCompany(companyDTO.toDAO());
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public String getAllUncheckCompany() throws Exception {
		String result = "";
		try {
			List<TbCompany> companyBeanList = this.companyDAO.getAllUnCheckCom();
			if(companyBeanList == null || companyBeanList.size() ==0)
				result = "";
			else{
				List<CompanyShowDTO> companyDTOList = new ArrayList<CompanyShowDTO>();
				for(TbCompany company:companyBeanList){
					companyDTOList.add(company.toShowDTO());
				}
				result = JSONArray.fromObject(companyDTOList).toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}

	@Override
	@Transactional
	public Integer checkCompanyInfo(Integer companyId, Integer companyStatus)
			throws Exception {
		Integer result = 0;
		try {
			result = this.companyDAO.checkCompanyInfo(companyId, companyStatus);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}
}
